package ge.telasi.tasks.ui.task;

import ge.telasi.tasks.model.Task;
import ge.telasi.tasks.model.TaskBody;
import ge.telasi.tasks.ui.UIUtils;
import ge.telasi.tasks.ui.comp.CommonDialog;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTabbedPane;

/**
 * @author dimitri
 */
public class TaskEditDialog extends CommonDialog {
    private static final long serialVersionUID = 1164953364758806848L;
    
    private TaskBodyList bodyList;
    private TaskHeaderPanel header;
    private TaskReceiverDisplayPanel pnAssignees;
    private TaskReceiverDisplayPanel pnCopies;
    private TaskRelationViewPanel pnRelation;
    private Task task;
    private TaskActionsPanel commentPane;
    private AttachmentPanel attachmentPanel;

    public TaskEditDialog(Component parent) {
        super(parent, true);
        initComponents();
        setSize(700, 500);
        header.addPropertyChangeListener(new PropertyChangeListener() {
            public void propertyChange(PropertyChangeEvent evt) {
                if ("task".equals(evt.getPropertyName())) {
                    task = (Task) evt.getNewValue();
                    resetHeader();
                    fireTaskChange();
                }
            }
        });
        commentPane.addPropertyChangeListener(new PropertyChangeListener() {
            public void propertyChange(PropertyChangeEvent evt) {
                if ("task".equals(evt.getPropertyName())) {
                    task = (Task) evt.getNewValue();
                    resetBodies();
                    fireTaskChange();
                }
            }
        });
        attachmentPanel.addPropertyChangeListener(new PropertyChangeListener() {
            public void propertyChange(PropertyChangeEvent evt) {
                if ("task".equals(evt.getPropertyName())) {
                    task = (Task) evt.getNewValue();
                    resetAttachments();
                    fireTaskChange();
                }
            }
        });
        pnRelation.addPropertyChangeListener(new PropertyChangeListener() {
            public void propertyChange(PropertyChangeEvent evt) {
                if ("task".equals(evt.getPropertyName())) {
                    task = (Task) evt.getNewValue();
                    resetBodies();
                    fireTaskChange();
                }
            }
        });
    }

    private void initComponents() {
        getContentPane().setLayout(new BorderLayout());
        getContentPane().add(createCenterComponent(), BorderLayout.CENTER);
        getContentPane().add(createHeader(), BorderLayout.NORTH);
    }

    private JComponent createHeader() {
        header = new TaskHeaderPanel();
        return header;
    }

    private JComponent createCenterComponent() {
        JTabbedPane tabs = new JTabbedPane();
        tabs.addTab("ძირითადი", UIUtils.getIcon16x16("email.png"), createMainTab());
        tabs.addTab("შემსრულებლები", UIUtils.getIcon16x16("group.png"), createReceiverTab());
        tabs.addTab("ფაილები", UIUtils.getIcon16x16("attach.png"), createFileTab());
        tabs.addTab("დამოკიდებულება", UIUtils.getIcon16x16("feed.png"), createRelationTab());
        return tabs;
    }

    private JComponent createMainTab() {
        JPanel mainTab = new JPanel(new BorderLayout());
        bodyList = new TaskBodyList();
        mainTab.add(new JScrollPane(bodyList), BorderLayout.CENTER);
        mainTab.add(createCommentPanel(), BorderLayout.SOUTH);
        return mainTab;
    }

    private JComponent createReceiverTab() {
        JPanel panel = new JPanel(new GridBagLayout());
        JSeparator jSeparator1 = new JSeparator();
        pnAssignees = new TaskReceiverDisplayPanel();
        pnCopies = new TaskReceiverDisplayPanel();
        GridBagConstraints c1 = new GridBagConstraints();
        GridBagConstraints c2 = new GridBagConstraints();
        GridBagConstraints c3 = new GridBagConstraints();
        c1.gridx = 0;
        c1.anchor = GridBagConstraints.FIRST_LINE_END;
        c1.insets = new Insets(5, 5, 5, 5);
        c2.gridx = 1;
        c2.insets = new Insets(5, 5, 5, 5);
        c2.weightx = 1;
        c2.weighty = 0.5;
        c2.fill = GridBagConstraints.BOTH;
        c3.gridx = 0;
        c3.gridwidth = 2;
        c3.weightx = 1;
        c3.fill = GridBagConstraints.HORIZONTAL;
        JLabel jLabel1 = new JLabel("შესრულება დაევალოს:");
        JLabel jLabel2 = new JLabel("ასლი გაეგზავნოს:");
        panel.add(jLabel1, c1);
        panel.add(pnAssignees, c2);
        panel.add(jSeparator1, c3);
        panel.add(jLabel2, c1);
        panel.add(pnCopies, c2);
        return panel;
    }

    private JComponent createFileTab() {
        attachmentPanel = new AttachmentPanel();
        return attachmentPanel;
    }

    private Component createRelationTab() {
        pnRelation = new TaskRelationViewPanel();
        return pnRelation;
    }

    public JComponent createCommentPanel() {
        commentPane = new TaskActionsPanel();
        return commentPane;
    }

    public Task getTask() {
        return task;
    }

    public void setTask(Task task) {
        this.task = task;
        resetHeader();
        resetBodies();
        pnAssignees.setTask(task, false);
        pnCopies.setTask(task, true);
        commentPane.setTask(task);
        attachmentPanel.setTask(task);
        pnRelation.setTask(task);
    }

    protected void resetHeader() {
        setTitle("დავალება №" + task.getNumber() + ": " + task.getSubject());
        header.setTask(task);
    }

    protected void resetBodies() {
        bodyList.display(task == null ? new ArrayList<TaskBody>() : task.getBodies());
    }

    protected void fireTaskChange() {
        firePropertyChange("task", "dummy string as old value", task);
    }

    protected void resetAttachments() {
        attachmentPanel.setTask(task);
    }
    
}
